export class Stack<T> {
  private items: T[] = [];

  // 入栈
  push(item: T): void {
    this.items.push(item);
  }

  // 出栈
  pop(): T | undefined {
    return this.items.pop();
  }

  // 查看栈顶元素
  peek(): T | undefined {
    return this.items[this.items.length - 1];
  }

  // 判断栈是否为空
  isEmpty(): boolean {
    return this.items.length === 0;
  }

  // 获取栈的大小
  size(): number {
    return this.items.length;
  }

  // 清空栈
  clear(): void {
    this.items = [];
  }
}
export class StackInstance {
  private static instance: Stack<string>;
  private constructor() {}

  public static getInstance(): Stack<string> {
    if (!StackInstance.instance) {
      StackInstance.instance = new Stack<string>();
    }
    return StackInstance.instance;
  }
}

export const stack = StackInstance.getInstance();
